/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bea.cliente.character;
import bea.cliente.environment.arena.Arena;
import bea.cliente.environment.WorldManager;
import bea.cliente.core.Game;
import bea.cliente.core.Memory;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.controls.ActionListener;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author Ivan
 */
public class CharacterAction implements ActionListener {

    // constantes de controle de acao
    public static final String LEFT_CLICK = "onLeftClick";
    public static final String RIGHT_CLICK = "onRightClick";
    // objeto da ação
    private Player player;
    private Game game;
    private WorldManager wManager;
    //private LocationMark collisionMarker;
    Geometry collisionMarker;

    public CharacterAction(Player player) {
        this.player = player;
        this.game = Memory.get("game");
        this.wManager = Memory.get("wManager");
    }

    @Override
    public void onAction(String binding, boolean keyPressed, float tpf) {
        System.out.println("onAction");
        if (binding.equals(LEFT_CLICK)) {
            if (keyPressed) {
                // aqui realiza o calculo para obter o ponto de colisão
                // do mouse com a cena
                Camera cam = game.getCamera();
                
                // ###################################################################################
                //  MODELO DE COLISÃO DO PICKER
                Arena sceneModel = wManager.getCurrentArena();
                // ################################################################################
                
                Vector3f origin = cam.getWorldCoordinates(game.getInputManager().getCursorPosition(), 0.0f);
                Vector3f direction = cam.getWorldCoordinates(game.getInputManager().getCursorPosition(), 0.3f);
                
                direction.subtractLocal(origin).normalizeLocal();
                
                Ray ray = new Ray(origin, direction);
                
                CollisionResults results = new CollisionResults();
                
                int numCollisions = sceneModel.collideWith(ray, results);
                if (numCollisions > 0) {
                    CollisionResult hit = results.getClosestCollision();
                    if (collisionMarker == null) {
                        createCollisionMarker();
                    }
                    Vector3f contactPoint = hit.getContactPoint().clone();
                    // atualiza posição do modelo de indicação do clique
                    collisionMarker.setLocalTranslation(contactPoint.clone());
                    // configura a altura da colisão igual a do player
                    // para evitar que o personagem incline o corpo ao andar
                    contactPoint.setY(player.getLocalTranslation().getY());
                    //
                    player.setTargetLocation(contactPoint);
                }
                 
            } else {
            }
        }
    }

    private void createCollisionMarker() {
        Sphere s = new Sphere(10, 10, 0.2f);
        collisionMarker = new Geometry("collisionMarker");
        collisionMarker.setMesh(s);
        Material mat = new Material(player.getGame().getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
        mat.setColor("m_Color", ColorRGBA.Magenta);
        collisionMarker.setMaterial(mat);
        game.getRootNode().attachChild(collisionMarker);
    }
}
